import os
from datetime import timedelta

class Config:
    """基础配置类"""
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev-secret-key-change-in-production'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    
    # 数据库连接池配置
    SQLALCHEMY_ENGINE_OPTIONS = {
        'pool_size': 5,  # 减少连接池大小
        'pool_recycle': 1800,  # 30分钟回收连接
        'pool_pre_ping': True,  # 连接前检查
        'pool_timeout': 30,  # 增加超时时间
        'max_overflow': 10,  # 减少最大溢出连接
        'connect_args': {
            'connect_timeout': 60,  # 连接超时
            'read_timeout': 60,  # 读取超时
            'write_timeout': 60,  # 写入超时
            'charset': 'utf8mb4',
            'autocommit': True
        }
    }
    
    JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY') or 'jwt-secret-key'
    JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=4)
    JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=30)
    """
    令牌过期设置：
        访问令牌有效期：1小时 (JWT_ACCESS_TOKEN_EXPIRES)
        刷新令牌有效期：30天 (JWT_REFRESH_TOKEN_EXPIRES)
    """
    # 分页配置
    ITEMS_PER_PAGE = 10

class DevelopmentConfig(Config):
    """开发环境配置"""
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
        'mysql+pymysql://root:longbhu1214@123.56.253.79:3306/rbac_dev'
    
    # CORS配置
    CORS_ORIGINS = [
        "http://localhost:3000",
        "http://localhost:5173",
        "http://127.0.0.1:3000",
        "http://127.0.0.1:5173"
    ]

class TestingConfig(Config):
    """测试环境配置"""
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or \
        'mysql+pymysql://root:password@localhost:3306/rbac_test'

class ProductionConfig(Config):
    """生产环境配置"""
    DEBUG = False
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
        'mysql+pymysql://root:password@localhost:3306/rbac_prod'
    
    # 生产环境CORS配置
    CORS_ORIGINS = [
        "https://yourdomain.com",
        "https://www.yourdomain.com"
    ]

# 配置字典
config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig
} 